<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $banner = Config::get('site.banner');//轮播图
        $affiche = Config::get('site.affiche');//公告
        //周期购商品
        $weekgoods = db('weekgoods')->where(['status'=>1])->order('createtime desc')->select();
        //全部商品
        $goods = db('products')->where([])->order('weigh desc')->select();
        $data['banner'] = $banner;
        $data['affiche'] = $affiche;
        $data['weekgoods'] = $weekgoods;
        $data['goods'] = $goods;
        $this->success('ok',$data);
    }
    //周期购商品详情
    public function weekgoodsinfo(){
        $weekgoods_id = input('weekgoods_id','');
        $info = db('weekgoods')->where(['id'=>$weekgoods_id])->find();
        $info['bannerimages'] = explode(',',$info['bannerimages']);
        $info['labeljson'] = json_decode($info['labeljson'],true);
        //判断是否收藏
        $token = input('token','');
        if($token){
            $user = $this->auth->getUser();
            $check = db('usercollect')->where(['user_id'=>$user->id,'type'=>1,'goods_id'=>$weekgoods_id])->find();
            if($check){
                $info['iscollect'] = true;
            }else{
                $info['iscollect'] = false;
            }
        }else{
            $info['iscollect'] = false;
        }
        $this->success('ok',$info);
    }
    //领取优惠券页面
    public function getcouponlist(){
        $list = db('coupon')->where(['status'=>1,'startday'=>['<=',date('Y-m-d')],'endday'=>['>=',date('Y-m-d')]])->select();
        $this->success('ok',$list);
    }
    //基地中心
    public function getarticlelist(){
        $page = input('page',1);
        $limit = input('limit',10);
        $search = input('search','');
        $where = [];
        if($search){
            $where['name'] = ['like','%'.$search.'%'];
        }
        $list = db('article')->where($where)->order('createtime desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i',$v['createtime']);
        }
        $this->success('ok',$list);
    }
    //文章详情
    public function getarticleinfo(){
        $id = input('id','');
        if(!$id){
            $this->error('缺失参数');
        }
        $info = db('article')->where(['id'=>$id])->find();
        db('article')->where(['id'=>$id])->setInc('looknum',1);
        $info['createtime'] = date('Y-m-d H:i',$info['createtime']);
        $this->success('ok',$info);
    }
}
